package driver

import (
	"context"

	"task/ecode"
	"task/models"
	pb "task/pb/dispatch"

	log "github.com/sirupsen/logrus"
)

// CallDispatch call dispatch.
func CallDispatch(ctx context.Context, event *models.DispatchEvent) error {
	var err error
	req := &pb.DispatchRequest{
		Event:       event.Event,
		ResourceId:  event.ResourceId,
		DagId:       int64(event.DagId),
		ProcessorId: int64(event.ProcessorId),
		Result:      event.Result,
	}
	resp := new(pb.DispatchResponse)
	if resp, err = DispatchClient.Dispatch(ctx, req); err != nil {
		log.Errorf("Dispatch rpc error(%v)", err)
		return ecode.RPCError
	}
	log.Infof("Dispatch rpc result(%+v)", resp.ProcessorIdList)
	return nil
}
